home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MSG Demo 1.2 Source / MSG Demo ƒ / MSG Shell ƒ / msg sounds.c < prev    next >
Text File  |  1993-11-10  |  3KB  |  147 lines

  1. /**********************************************************************\
  2.  
  3. File:        msg sounds.c
  4.  
  5. Purpose:    This module handles playing asynchronous sounds.  Also,
  6.             a modal dialog filter that lets the user cough to continue
  7.             (if an internal microphone is available).
  8.  
  9.  
  10. MSG Demo -- graphic effects demonstration program
  11. Copyright (C) 1992-3 Mark Pilgrim & Dave Blumenthal
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg sounds.h"
  31. #include "msg graphics.h"
  32.  
  33. SndChannelPtr        myChannel;
  34. Handle                MySounds[NUM_SOUNDS];
  35. Boolean                gSoundToggle;
  36. Boolean                gSoundAvailable;
  37.  
  38. #include "SoundInput.h"
  39.  
  40. static long        gSoundInputRefNum;
  41.  
  42. OSErr OpenTheSoundDevice(void)
  43. {
  44.     OSErr isHuman;
  45.     short meterState = 1;
  46.     
  47.     if (!(isHuman = SPBOpenDevice(0L, siWritePermission, &gSoundInputRefNum)))
  48.         isHuman = SPBSetDeviceInfo(gSoundInputRefNum, siLevelMeterOnOff, &meterState);
  49.     return isHuman;
  50. }
  51.  
  52. void CloseTheSoundDevice(void)
  53. {
  54.     SPBCloseDevice(gSoundInputRefNum);
  55.     gSoundInputRefNum = 0;
  56. }
  57.  
  58. pascal Boolean ProcOFilter(DialogPtr dialog, EventRecord *event, short *item)
  59. {
  60.     unsigned char    theKey;
  61.     OSErr            isHuman;
  62.     short            recordingStatus = 0;
  63.     short            meterLevel = 0;
  64.     unsigned long    totalSamplesToRecord = 0;
  65.     unsigned long    numberOfSamplesRecorded = 0;
  66.     unsigned long    totalMsecsToRecord;
  67.     unsigned long    numberOfMsecsRecorded;
  68.     
  69.     if(gSoundInputRefNum)
  70.     {
  71.         isHuman = SPBGetRecordingStatus(gSoundInputRefNum, &recordingStatus, &meterLevel,
  72.                                     &totalSamplesToRecord, &numberOfSamplesRecorded,
  73.                                     &totalMsecsToRecord,&numberOfMsecsRecorded);
  74.         
  75.         if (meterLevel > 250)
  76.         {
  77.             *item=1;
  78.             return TRUE;
  79.         }
  80.     }
  81.     
  82.     switch(event->what)
  83.     {
  84.         case keyDown:
  85.         case autoKey:
  86.             theKey = event->message & charCodeMask;
  87.             
  88.             if (theKey == 0x0D || theKey == 0x03 || theKey == 0x1B)
  89.             {
  90.                 *item=1;
  91.                 return TRUE;
  92.             }
  93.     }
  94.     return FALSE;
  95. }
  96.  
  97. void InitSounds(void)
  98. {
  99.     int                i;
  100.     
  101.     gSoundAvailable=(SndNewChannel(&myChannel, 0, 0L, 0L)==noErr);
  102.     if (!gSoundAvailable)
  103.         myChannel=0;
  104.     for (i=0; i<NUM_SOUNDS; i++)
  105.         MySounds[i]=0L;
  106. }
  107.  
  108. void DoSound(int whichSound, Boolean async)
  109. {
  110.     SndCommand        myCommand;
  111.     OSErr            isHuman;
  112.     Str255            tempStr;
  113.     
  114.     if (myChannel!=0)
  115.     {
  116.         SndDisposeChannel(myChannel, TRUE);
  117.         myChannel=0;
  118.     }
  119.     
  120.     whichSound-=1000;
  121.     if ((gSoundToggle) && (gSoundAvailable))
  122.     {
  123.         if (!MySounds[whichSound])
  124.             MySounds[whichSound]=GetResource('snd ', whichSound+1000);
  125.         
  126.         if (MySounds[whichSound])
  127.         {
  128.             if(SndNewChannel(&myChannel, 0, 0L, 0L) != noErr)                    
  129.             {
  130.                 myChannel = 0;
  131.                 gSoundAvailable = FALSE;
  132.             }
  133.             else SndPlay(myChannel, MySounds[whichSound], async);
  134.         }
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. void CloseSounds(void)
  141. {
  142.     int                i;
  143.     
  144.     if(myChannel)
  145.         SndDisposeChannel(myChannel, TRUE);
  146. }
  147.